// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose:  Definition of CXFormsDebugAgent.
//   $Id$


#pragma once
#include "stdafx.h"
#include "resource.h"
#include <comsvcs.h>
#include "XForms.h"
#include <Common/COM/msw/MultiIDispatch.h>
#include "FormControl.h"

typedef IDispatchImpl<IXFormsDebugAgent, &IID_IXFormsDebugAgent, &LIBID_XFORMSLib, /*wMajor =*/ 1, /*wMinor =*/ 16> XFormsDebugAgentDispatch;
typedef IDispatchImpl<IXFormsInfoSet, &__uuidof(IXFormsInfoSet), &LIBID_XFORMSLib, /* wMajor = */ 1, /* wMinor = */ 16> XFormsInfoSetDispatch;
typedef IDispatchImpl<IXFormsInfoSet2, &__uuidof(IXFormsInfoSet2), &LIBID_XFORMSLib, /* wMajor = */ 1, /* wMinor = */ 16> XFormsInfoSetDispatch2;
typedef IDispatchImpl<IXFormsInfoSet3, &__uuidof(IXFormsInfoSet3), &LIBID_XFORMSLib, /* wMajor = */ 1, /* wMinor = */ 16> XFormsInfoSetDispatch3;
typedef IDispatchImpl<IXFormsInfoSet4, &__uuidof(IXFormsInfoSet4), &LIBID_XFORMSLib, /* wMajor = */ 1, /* wMinor = */ 16> XFormsInfoSetDispatch4;
class CXFormsDebugAgent;
typedef CMultiIDispatch<CXFormsDebugAgent> CXFormsDebugAgentMultiIdispatch;

class ATL_NO_VTABLE CXFormsDebugAgent : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public ISupportErrorInfo,
	public IObjectSafetyImpl<CXFormsDebugAgent, INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA>,
	public CComCoClass<CXFormsDebugAgent, &CLSID_XFormsDebugAgent>,
	public XFormsDebugAgentDispatch,
	public XFormsInfoSetDispatch,
	public XFormsInfoSetDispatch2,
	public XFormsInfoSetDispatch3,
	public XFormsInfoSetDispatch4,
	public CXFormsDebugAgentMultiIdispatch
{
public:
	CXFormsDebugAgent();
	virtual ~CXFormsDebugAgent();

	DECLARE_MULTI_DISPATCH(CXFormsDebugAgent)

	BEGIN_MULTI_DISPATCH_MAP(CXFormsDebugAgent)
		MULTI_DISPATCH_ENTRY(XFormsDebugAgentDispatch)
		MULTI_DISPATCH_ENTRY(XFormsInfoSetDispatch)
		MULTI_DISPATCH_ENTRY(XFormsInfoSetDispatch2)
		MULTI_DISPATCH_ENTRY(XFormsInfoSetDispatch3)
		MULTI_DISPATCH_ENTRY(XFormsInfoSetDispatch4)
	END_MULTI_DISPATCH_MAP()         

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease() 
	{
	}

	DECLARE_REGISTRY_RESOURCEID(IDR_XFORMSDEBUGAGENT)

	//DECLARE_NOT_AGGREGATABLE(CXFormsDebugAgent)

	BEGIN_COM_MAP(CXFormsDebugAgent)
		COM_INTERFACE_ENTRY(ISupportErrorInfo)
		COM_INTERFACE_ENTRY(IObjectSafety)
		COM_INTERFACE_ENTRY(IXFormsDebugAgent)
		COM_INTERFACE_ENTRY(IXFormsInfoSet)
		COM_INTERFACE_ENTRY(IXFormsInfoSet2)
		COM_INTERFACE_ENTRY(IXFormsInfoSet3)
		COM_INTERFACE_ENTRY(IXFormsInfoSet4)
		COM_INTERFACE_ENTRY2(IDispatch, IXFormsDebugAgent)
	END_COM_MAP()

	// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid)
	{
		static const IID* arr[] = 
		{
			&IID_IXFormsDebugAgent
		};
		for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
		{
			if (InlineIsEqualGUID(*arr[i], riid))
				return S_OK;
		}
		return S_FALSE;
	}

	// IXFormsInfoSet Methods
	STDMETHOD(getForms)(IXMLDOMDocument **ppFormsDOM);
	STDMETHOD(getModel)(BSTR sFormId, BSTR  sModelId, IXFormsModelElement **ppModel);
	STDMETHOD(getControls)(BSTR sFormId, IXMLDOMDocument **ppControlsDOM);
	STDMETHOD(getControlInformation)(BSTR sFormId, BSTR sControlId, IXMLDOMDocument **ppControlInformationDOM);

	// IXFormsInfoSet2 Methods
	STDMETHOD(getFormsWithOmnipresentIds)(IXMLDOMDocument **ppFormsDOM);
	STDMETHOD(getModelUsingOmnipresentIds)(BSTR sFormId, BSTR  sModelId, IXFormsModelElement **ppModel);
	STDMETHOD(getControlsWithOmnipresentIds)(BSTR sFormId, IXMLDOMDocument **ppControlsDOM);
	STDMETHOD(getControlInformationUsingOmnipresentIds)(BSTR sFormId, BSTR sControlId, IXMLDOMDocument **ppControlInformationDOM);
	STDMETHOD(getSourceIdFromOmnipresentId)(BSTR sId, BSTR *psSourceId);

	// IXFormsInfoSet3 Methods
	STDMETHOD(getInstanceDocument)(BSTR sId, IXMLDOMDocument **ppDoc);
	STDMETHOD(putInstanceDocument)(BSTR sId, IXMLDOMDocument *pDoc);

	// IXFormsInfoSet3 Methods
	STDMETHOD(getInstanceDocumentWithLiteralId)(BSTR sId, IXMLDOMDocument **ppDoc);
	STDMETHOD(putInstanceDocumentWithLiteralId)(BSTR sId, IXMLDOMDocument *pDoc);

private:
	// Search for an instance with a unique id that matches sId. This method is slow.
	HRESULT FindInstance(BSTR sId, bool bUseOmnipresentIds, CImplInstance **ppInstance) const;
};

OBJECT_ENTRY_AUTO(__uuidof(XFormsDebugAgent), CXFormsDebugAgent)
